/*
 * SConsts.java
 *
 * Created on 8.4.2010, 11:25:53
 */
package gui;

import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.SpinnerNumberModel;
import psim.Main;

/**
 * Trida, ktera vytvari okno, ve kterem lze nastavovat
 * globalni konstanty.
 * @author Tomas Drimal
 */
public class SConsts extends javax.swing.JDialog {

    private float pomer;
    /** Creates new form SConsts */
    public SConsts() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        tfieldDt = new javax.swing.JTextField();
        btnApply = new javax.swing.JButton();
        btnOK = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator2 = new javax.swing.JSeparator();
        chbReboundWalls = new javax.swing.JCheckBox();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        tfRealWidth = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        tfRealHeight = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jSeparator3 = new javax.swing.JSeparator();
        spWidth = new javax.swing.JSpinner();
        spHeight = new javax.swing.JSpinner();

        setTitle("Set global variables");
        setAlwaysOnTop(true);
        setResizable(false);
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });

        jLabel1.setText("dt [s]: ");

        tfieldDt.setText("jTextField1");
        tfieldDt.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tfieldDtFocusLost(evt);
            }
        });

        btnApply.setText("Apply changes");
        btnApply.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnApplyActionPerformed(evt);
            }
        });

        btnOK.setText("OK");
        btnOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOKActionPerformed(evt);
            }
        });

        chbReboundWalls.setText("Rebounding of walls");
        chbReboundWalls.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chbReboundWallsActionPerformed(evt);
            }
        });

        jLabel2.setText("Real space size [m]");

        jLabel3.setText("Simulation space size [px]");

        tfRealWidth.setText("w");
        tfRealWidth.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tfRealWidthFocusLost(evt);
            }
        });

        jLabel4.setText("Width: ");

        tfRealHeight.setText("h");
        tfRealHeight.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tfRealHeightFocusLost(evt);
            }
        });

        jLabel5.setText("Height: ");

        jLabel6.setText("Height: ");

        jLabel7.setText("Width: ");

        jSeparator3.setOrientation(javax.swing.SwingConstants.VERTICAL);

        spWidth.setModel(new SpinnerNumberModel(Main.CLIENT_WIDTH_PX, //initial value
            5, //min
            3000, //max
            1));

    spHeight.setModel(new SpinnerNumberModel(Main.CLIENT_HEIGHT_PX, //initial value
        5, //min
        3000, //max
        1));

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
        .addGap(40, 40, 40)
        .addComponent(jLabel1)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(tfieldDt, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addGap(89, 89, 89)
        .addComponent(chbReboundWalls)
        .addContainerGap(76, Short.MAX_VALUE))
    .addGroup(layout.createSequentialGroup()
        .addGap(37, 37, 37)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel2)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(jLabel5)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(tfRealHeight, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createSequentialGroup()
                    .addComponent(jLabel4)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(tfRealWidth, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))))
        .addGap(55, 55, 55)
        .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addGap(18, 18, 18)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel3)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(7, 7, 7)
                        .addComponent(jLabel7))
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(spHeight)
                    .addComponent(spWidth, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE))))
        .addGap(54, 54, 54))
    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
        .addContainerGap(324, Short.MAX_VALUE)
        .addComponent(btnApply)
        .addGap(18, 18, 18)
        .addComponent(btnOK)
        .addGap(21, 21, 21))
    .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 474, Short.MAX_VALUE)
        .addContainerGap())
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 474, Short.MAX_VALUE)
            .addContainerGap()))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                    .addComponent(jLabel2)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(tfRealWidth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel4))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(tfRealHeight, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel5)))
                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                    .addComponent(jLabel3)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel7)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(spWidth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(spHeight, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel6)))))
                .addComponent(jSeparator3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 12, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(tfieldDt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel1)
                .addComponent(chbReboundWalls))
            .addGap(36, 36, 36)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(btnApply)
                .addComponent(btnOK))
            .addContainerGap())
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(218, Short.MAX_VALUE)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(49, 49, 49)))
    );

    pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnApplyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnApplyActionPerformed

        Main.dt = Double.valueOf(tfieldDt.getText());
        psim.Main.bound = chbReboundWalls.isSelected();
        psim.Main.scale_x = Double.valueOf(tfRealWidth.getText());
        psim.Main.scale_y = Double.valueOf(tfRealHeight.getText());
        // osetreni novych hodnot v polickach tfClientWidth a tfClientHeight
        if (psim.Main.CLIENT_WIDTH_PX
                != Integer.valueOf(spWidth.getModel().getValue().toString())
                || psim.Main.CLIENT_HEIGHT_PX
                != Integer.valueOf(spHeight.getModel().getValue().toString())) {
            psim.Main.CLIENT_WIDTH_PX =
                    Integer.valueOf(spWidth.getModel().getValue().toString());
            psim.Main.CLIENT_HEIGHT_PX =
                    Integer.valueOf(spHeight.getModel().getValue().toString());
            gui.Gui.platno.init();
        }

    }//GEN-LAST:event_btnApplyActionPerformed

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        tfieldDt.setText(Double.toString(Main.dt));
        tfRealHeight.setText(Double.toString(psim.Main.scale_y));
        tfRealWidth.setText(Double.toString(psim.Main.scale_x));
        chbReboundWalls.setSelected(psim.Main.bound);
        tfieldDt.requestFocusInWindow();
        this.setLocationRelativeTo(null);
    }//GEN-LAST:event_formComponentShown

    private void btnOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOKActionPerformed
        btnApplyActionPerformed(evt);
        this.setVisible(false);
    }//GEN-LAST:event_btnOKActionPerformed

    private void chbReboundWallsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chbReboundWallsActionPerformed
        if (chbReboundWalls.isSelected()) {
            JOptionPane pane = new JOptionPane(
                    "If you select this option, the particles,\n"
                    + "which are out of visible space will be \n"
                    + "moved into the visible space. \n"
                    + "Are you shure?");
            Object[] options = new String[]{"Yes", "No"};
            pane.setOptions(options);
            JDialog dialog = pane.createDialog(this, "Dilaog");
            dialog.show();
            Object obj = pane.getValue();
            int result = -1;
            for (int k = 0; k < options.length; k++) {
                if (options[k].equals(obj)) {
                    result = k;
                }
            }
            if (result == 0) {
                chbReboundWalls.setSelected(true);
            } else {
                chbReboundWalls.setSelected(false);
            }
        }
    }//GEN-LAST:event_chbReboundWallsActionPerformed

    private void tfRealWidthFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tfRealWidthFocusLost
        try {
            Double.valueOf(tfRealWidth.getText());
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Invalid value! Must be a number!");
            tfRealWidth.requestFocusInWindow();
        }
    }//GEN-LAST:event_tfRealWidthFocusLost

    private void tfRealHeightFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tfRealHeightFocusLost
        try {
            Double.valueOf(tfRealHeight.getText());
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Invalid value! Must be a number!");
            tfRealHeight.requestFocusInWindow();
        }
    }//GEN-LAST:event_tfRealHeightFocusLost

    private void tfieldDtFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tfieldDtFocusLost
        try {
            Double.valueOf(tfieldDt.getText());
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Invalid value of dt! Must be a number!");
            tfieldDt.requestFocusInWindow();
        }
    }//GEN-LAST:event_tfieldDtFocusLost

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnApply;
    private javax.swing.JButton btnOK;
    private javax.swing.JCheckBox chbReboundWalls;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JSpinner spHeight;
    private javax.swing.JSpinner spWidth;
    private javax.swing.JTextField tfRealHeight;
    private javax.swing.JTextField tfRealWidth;
    private javax.swing.JTextField tfieldDt;
    // End of variables declaration//GEN-END:variables
}
